<?php

define('IN_MEDIA',true);

include('includes/config.php');
include('includes/functions.php');
$isLoggedIn = m_checkLogin();

if (!$isLoggedIn && !$_POST['rating']) die("<center><b>Bạn chưa đăng nhập</b></center>");



if ($_POST['rating'] && $_POST['media_id'] && $_POST['star']) {
	//if (!$isLoggedIn) echo "<br>&nbsp; &nbsp; <b>Bạn chưa đăng nhập!</b>";
	//else{
	$id = (int)$_POST['media_id'];

	$star = (int)$_POST['star'];

	if ($isLoggedIn || !(m_get_config('must_login_to_rate'))) {

		  $m_whorate = m_get_data('SONG',$id,'m_whorate');

            $z = split(',',$m_whorate);

            if (in_array($_SESSION['user_id'],$z))

            echo "<b>Bạn đã bình chọn rồi mà!<b><br>";

            else

		$mysql->query("UPDATE ".$tb_prefix."data SET m_rating = m_rating+$star, m_rating_total = m_rating_total+1,m_rating_week = m_rating_week+$star, m_rating_total_week = m_rating_total_week+1, m_whorate = CONCAT('".$_SESSION['user_id'].",',m_whorate) WHERE m_id = $id");

	}

	$q = $mysql->query("SELECT m_rating, m_rating_total FROM ".$tb_prefix."data WHERE m_id = $id");

	$q = $mysql->fetch_array($q);

	if ($q['m_rating_total'] =='0') $current_star = 0;

	else $rater_rating = $q['m_rating'] / $q['m_rating_total'];

	

	// Assign star image

	 if ($rater_rating <= 0  ){$star1 = "star_n"; $star2 = "star_n"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 0.5){$star1 = "star_h"; $star2 = "star_n"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 1  ){$star1 = "star"; $star2 = "star_n"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 1.5){$star1 = "star"; $star2 = "star_h"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 2  ){$star1 = "star"; $star2 = "star"; $star3 = "star_n"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 2.5){$star1 = "star"; $star2 = "star"; $star3 = "star_h"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 3  ){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star_n"; $star5 = "star_n";}
    if ($rater_rating >= 3.5){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star_h"; $star5 = "star_n";}
    if ($rater_rating >= 4  ){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star"; $star5 = "star_n";}
    if ($rater_rating >= 4.5){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star"; $star5 = "star_h";}
    if ($rater_rating >= 5  ){$star1 = "star"; $star2 = "star"; $star3 = "star"; $star4 = "star"; $star5 = "star";}



	echo "<img src=\"$mainURL/templates/ClassicBlack/images/".$star1.".gif\">"
                        ." <img src=\"$mainURL/templates/ClassicBlack/images/".$star2.".gif\">"
                        ." <img src=\"$mainURL/templates/ClassicBlack/images/".$star3.".gif\">"
                        ." <img src=\"$mainURL/templates/ClassicBlack/images/".$star4.".gif\">"
                        ." <img src=\"$mainURL/templates/ClassicBlack/images/".$star5.".gif\">"
						."".$q['m_rating_total']." Bình chọn";

	//}
}

?>